﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Ycjw.PipeLine.WPF
{
    /// <summary>
    /// BForm.xaml 的交互逻辑
    /// </summary>
    public partial class BForm : Window
    {
        IResposeWPF m_IResposeWPF;
        public BForm(IResposeWPF respose)
        {
            InitializeComponent();
            Init();
            m_IResposeWPF = respose;
        }
        public System.Windows.Shapes.Ellipse EllipseAll
        {
            get
            {
                return m_EllipseAll;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseWS
        {
            get
            {
                return m_EllipseWS;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseYS
        {
            get
            {
                return m_EllipseYS;
            }
        }
        void Init()
        {
            var bitmap = new Bitmap(Commons.Paths.ApplicationResourcesDir + "\\exit.png"); ;
            m_ImageExist.Source = BitmapToBitmapSource(bitmap) ;
            m_ImageExist.MouseUp += ImageExist_MouseUp;

            bitmap = new Bitmap(Commons.Paths.ApplicationResourcesDir + "\\home.png"); ;
            m_ImageHome.Source = BitmapToBitmapSource(bitmap);
            m_ImageHome.MouseUp += ImageHome_MouseUp;

            m_EllipseWS.MouseEnter += Ellipse_MouseEnter;
            m_EllipseYS.MouseEnter += Ellipse_MouseEnter;
            m_EllipseAll.MouseEnter += Ellipse_MouseEnter;

            m_EllipseWS.MouseLeave += Ellipse_MouseLeave;
            m_EllipseYS.MouseLeave += Ellipse_MouseLeave;
            m_EllipseAll.MouseLeave += Ellipse_MouseLeave;
        }


        private void Ellipse_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var ell = sender as System.Windows.Shapes.Ellipse;
            if (m_EllipseOpacity == -1) return;
            ell.Opacity = m_EllipseOpacity;
        }

        double m_EllipseOpacity = -1;
        private void Ellipse_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var ell = sender as System.Windows.Shapes.Ellipse;
            if (m_EllipseOpacity == -1) m_EllipseOpacity = ell.Opacity;
            ell.Opacity = 1;
        }

        private void ImageHome_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.m_IResposeWPF.ResposeWPF("VIEWENTIRE");
        }

        private void ImageExist_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Hide();
        }

        public DockPanel StackPanel01
        {
            get { return this.m_PanelLeftCahrt01; }
        }
        public DockPanel StackPanel02
        {
            get { return this.m_PanelLeftCahrt02; }
        }
        public DockPanel StackPanel03
        {
            get { return this.m_PanelLeftCahrt03; }
        }
        public DockPanel StackPanel04
        {
            get { return this.m_PanelLeftCahrt04; }
        }

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        //将 Bitmap转换为BitmapSource
        //使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象，不然内存不释放，很快系统内存就消耗光了。
        private static ImageSource BitmapToBitmapSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
            {
                throw new System.ComponentModel.Win32Exception();
            }

            return wpfBitmap;
        }
    }
}
